//
//  FeedBackViewController.m
//  体育
//
//  Created by  on 2022/10/25.
//

#import "FeedBackViewController.h"
#import "AddCell.h"

@interface FeedBackViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UITextView *input;
@property (strong, nonatomic) IBOutlet UICollectionView *collection;
@property (strong, nonatomic) IBOutlet UILabel *placeholder;
@property (strong, nonatomic)  NSMutableArray *imgs;

@end

@implementation FeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)initUI{
    self.imgs = [NSMutableArray array];
    self.placeholder.text = self.title;
    [self.collection registerNib:[AddCell nib] forCellWithReuseIdentifier:[AddCell ID]];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (self.input.text.length<=0) {
        return [CCHud msg:LS(@"内容不能为空")];
    }else if (self.imgs.count <= 0){
        return [CCHud msg:LS(@"至少选择一张图片")];
    }
    if (self.model) {
        self.model.remark =  self.input.text;
        [self.model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [NotifyTool post:notifyRecordUpdate :nil];
            });
        }];
        [CCHud startHud:LS(@"保存成功") stopTime:1 handle:^{
            [self backBtnAction];
        }];
    }else{
        [self backBtnAction];
    }
}
#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    self.placeholder.hidden = YES;
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView{
    self.placeholder.hidden = textView.text.length > 0;
}
#pragma mark - UIImagePickerControllerDelegate / UINavigationControllerDelegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    UIImage *img = info[UIImagePickerControllerEditedImage];
    NSData *data = UIImageJPEGRepresentation(img, 0.5);
    if (self.model) {
        [self.model.imgs addObject:data];
    }
    [self.imgs addObject:data];
    [self.collection reloadData];
    [self.collection scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.imgs.count inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imgs.count + 1;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    AddCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[AddCell ID] forIndexPath:indexPath];
    cell.img.image = indexPath.row == self.imgs.count ? [UIImage imageNamed:@"add_蓝色"] : [UIImage imageWithData:self.imgs[indexPath.row]];
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.imgs.count) {
        WS(self)
        SheetViewController *sheetvc = [SheetViewController new];
        sheetvc.withCancel = NO;
        sheetvc.array = @[@"拍摄",@"相册"];
        sheetvc.block = ^(int index, NSString * _Nonnull text) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [CCHud startHud];
            });
            UIImagePickerController *vc = [UIImagePickerController new];
            vc.delegate = weakself;
            vc.allowsEditing = YES;
            vc.sourceType = index == 0 ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;
            vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
            [[weakself currentVC] presentViewController:vc animated:YES completion:^{
                [CCHud stopHud];
            }];
        };
        [[weakself currentVC] presentViewController:sheetvc animated:NO completion:nil];
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake(150,150);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 20, 20, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}

@end
